@echo off
chcp 65001 > nul
title UC System Launcher

echo ====================================
echo     UC System Auto Start Script
echo ====================================
echo.

[1/6] Check Python environment...
python --version > nul 2>&1
if errorlevel 1 (
    echo [ERROR] Python not installed or not in PATH
    echo Please install Python 3.8+
    pause
    exit /b 1
)
echo [OK] Python environment ready

echo.
echo [2/6] Check Node.js environment...
node --version > nul 2>&1
if errorlevel 1 (
    echo [ERROR] Node.js not installed or not in PATH
    echo Please install Node.js 16+
    pause
    exit /b 1
)
echo [OK] Node.js environment ready

echo.
echo [3/6] Check model files...
if not exist "outputs\working_training\best_model.pth" (
    echo [WARNING] Trained model file not found
    echo Will use pretrained model
) else (
    echo [OK] Found trained model file
)

echo.
echo [4/6] Starting AI Inference Service...
cd /d "%~dp0endo_sight_uc_ai"
start "AI Service" cmd /k "python inference_server.py"
timeout /t 3 /nobreak > nul

echo.
echo [5/6] Starting Backend API Service...
cd /d "%~dp0endo_sight_uc_server"
start "Backend Service" cmd /k "npm run start-with-env"
timeout /t 5 /nobreak > nul

echo.
echo [6/6] Starting Frontend Web Service...
cd /d "%~dp0endo_sight_uc_web"
start "Frontend Service" cmd /k "npm run dev"

cd /d "%~dp0"

echo.
echo ====================================
echo     Services Started Successfully!
echo ====================================
echo.
echo Service URLs:
echo - Frontend: http://localhost:8101
echo - Backend API: http://localhost:4000/api/health
echo - AI Service: http://localhost:5001/health
echo.
echo Tips:
echo 1. Please wait 30 seconds for all services to fully start
echo 2. If port errors occur, check if ports are occupied
echo 3. Closing this window will not stop services
echo 4. To stop all services, run stop_system.bat
echo.
echo Press any key to open browser...
pause > nul

start http://localhost:8101

echo.
echo System is running in background. Enjoy!
echo.
pause
